// NWS - NMC

var PlayScene = cc.Layer.extend({
    isMouseDown: false,
    mainsprite: null,
    sprite: null,

    ctor: function () {
        this._super();
        var me = this;
        daifugo.playscene = this;
        //daifugo.serverHandler.socket.on('message', daifugo.serverHandler.Processing);
        me.init();
    },
    init: function () {
        this._super();
        // Tempo Data
        //        Card.Color = {
        //            c: 0, // Clubs
        //            d: 1, // Diamonds
        //            h: 2, // Hearts
        //            s: 3, // Spades
        //            j: 4  // Rank
        //        }
        /***********************************
         *   Card object to play a game
         *
         *   1 : Ace
         *   2 <= i <= 10 : card rank i-th
         *   11: Jack
         *   12: Queen
         *   13: King
         *   14: Rank
         ************************************/
        // Number of Rank card
        //Card.Rank = 14;


        var me = this;
        var size = cc.Director.getInstance().getWinSize();

        //me.imgInfo = cc.Sprite.create(g_imageRes.info);
        //me.imgInfo.setPosition(size.width / 2, size.height / 2);
        me.mainsprite = cc.Sprite.create(g_imageRes.s_bg);
        me.mainsprite.setPosition(size.width / 2, size.height / 2);
        me.addChild(this.mainsprite);
        //var btn_okSprite = cc.Sprite.create(g_imageRes.btn_ok);
        //me.btnOK = cc.ControlButton.create(btn_okSprite);
        //me.btnOK.setTouchPriority(0);
        //me.btnOK.setAdjustBackgroundImage(true);
        //me.btnOK.setPosition(size.width / 2, size.height * (170 / 720));
        //me.btnOK.addTargetWithActionForControlEvents(this, function () {
        //    playSound('tap');
        //    daifugo.serverHandler.contactServer('message', function () {
        //    }, {action: 'find_table'});
        //    me.imgInfo.removeFromParent();
        //    me.btnOK.removeFromParent();
        //}, cc.CONTROL_EVENT_TOUCH_DOWN);
        //me.addChild(me.imgInfo);
        //me.addChild(me.btnOK);

//-------------------------------------------//

    },

    // Draw User Card
    //-------------------------------------------------------------------//
    drawCards: function () {
        var me = this;
        var size = cc.Director.getInstance().getWinSize();
        var mWidthIconUser = size.width - size.width * (92 / 960);
        var mHeightIconUser = size.height / 2;
        var mHeightButton = size.height * (47 / 720);
        var mWidthButtonPass = size.width - size.width * (106 / 960);
        var mWidthButtonCard = mWidthButtonPass - size.width * (181 / 960);
        var mWidthTopTableCard = size.width * (442.5 / 960);
        var mHeightTopTableCard = size.height * (269 / 720);
        var mArrButtonCard = [];
        this.ArrayButtonCard = mArrButtonCard;
        this.mArrCheck = [];
        this.mTurnCard = [];
        for (var i = 0; i < this.myCards.length; i++) {
            playSound('deal_card');
            var c = this.myCards[i].mColor, r = this.myCards[i].mRank;
            var mSprOvalCheck = cc.Sprite.create(g_imageRes.s_ovalcheck);
            var mOverLayer = cc.Sprite.create(g_imageRes.s_overlayer);
            var mSprCard = cc.Sprite.createWithSpriteFrameName(convertColorCard(c) + "_" + convertRankCard(r) + ".png");
            var wCard = getSizeCCNode(mSprCard).getWidth() + 2;
            var hCard = getSizeCCNode(mSprCard).getHeight() + 2;
            mArrButtonCard[i] = cc.ControlButton.create(mSprCard);
            mArrButtonCard[i].setTouchPriority(0);
            mArrButtonCard[i].opacity = mSprOvalCheck;
            mArrButtonCard[i].overlayer = mOverLayer;
            mArrButtonCard[i].c = c;
            mArrButtonCard[i].index = i;
            mArrButtonCard[i].r = r;
            mArrButtonCard[i].w = getSizeCCNode(mSprCard).getWidth() / 2;
            mArrButtonCard[i].h = getSizeCCNode(mSprCard).getHeight() / 2;
            mArrButtonCard[i].setAdjustBackgroundImage(true);
            mArrButtonCard[i].setPosition(size.width / 2 - 40, size.height / 2 + 70);
            mArrButtonCard[i].runAction(cc.MoveTo.create(1, cc.p(mWidthTopTableCard + wCard * ((i % 7) - size.width / 960), mHeightTopTableCard - hCard * Math.floor(i / 7))));
            this.addChild(mArrButtonCard[i]);
            var MoveBy = cc.MoveTo.create(1, cc.p(size.width / 2 + 50, size.height / 2 + 70));

            mArrButtonCard[i].addChild(mOverLayer);
            mArrButtonCard[i].addChild(mSprOvalCheck);
            me.ArrayButtonCard[i].deal = false;

            mOverLayer.setPosition(
                mArrButtonCard[i].w,
                mArrButtonCard[i].h
            );
            mSprOvalCheck.setVisible(false);
            mSprOvalCheck.setPosition(
                mArrButtonCard[i].w,
                mArrButtonCard[i].h
            );
            //--------------/
        }
        for (var i = 0; i < me.ArrayButtonCard.length; i++) {
            me.ArrayButtonCard[i].setTouchEnabled(false);
        }
        /*
         *
         * Icon User
         * */
        var mSprUser = cc.Sprite.create(g_imageRes.s_player_user);
        mSprUser.setPosition(mWidthIconUser, mHeightIconUser);
        this.addChild(mSprUser);


        /*
         *
         *
         * Card Button
         *
         * */
        var mBtnCardSprite = cc.Sprite.create(g_imageRes.s_btn_card);
        me.mButtonCard = cc.ControlButton.create(mBtnCardSprite);
        me.mButtonCard.setTouchPriority(0);
        me.mButtonCard.setAdjustBackgroundImage(true);
        me.mButtonCard.setPosition(mWidthButtonCard, mHeightButton);
        this.addChild(me.mButtonCard);


        me.mButtonCard.addTargetWithActionForControlEvents(this, function () {
            //if (!me.canbeAction) { // TODO: trick before server
            //    return;
            //}
            me.canbeAction = false;
            me.mTurnCard = [];
            me.dealedCards = [];
            for (var i = 0; i < me.dealedCards.length; i++) {
                me.dealedCards[i].setZOrder(1);
            }

            var newArrayButtonCard = [];
            for (var i = 0; i < me.ArrayButtonCard.length; i++) {
                if (me.ArrayButtonCard[i].opacity._visible == false) {
                    newArrayButtonCard.push(me.ArrayButtonCard[i]);
                }
                else {
                    me.dealedCards.push(me.ArrayButtonCard[i]);
                    me.ArrayButtonCard[i].opacity.setVisible(false);
                }
            }

            if (me.dealedCards.length > 0) {
                me.mArrExchange = [];
                sortByIndex(me.dealedCards);
                var myiSolution = checkArrExistence(me.dealedCards, me.mSolutionCard);

                if (myiSolution != null) {
                    for (var i = 0; i < me.dealedCards.length; i++) {
                        me.dealedCards[i].setZOrder(2);
                        //actionMoveCard(me.dealedCards[i], i);
                        me.mArrExchange[i] = me.dealedCards[i].index;
                        me.dealedCards[i].setEnabled(false);
                    }

                    me.ArrayButtonCard = newArrayButtonCard;
                }
                else {

                    me.dealedCards = []
                    for (var i = 0; i < me.ArrayButtonCard.length; i++) {
                        me.ArrayButtonCard[i].opacity.setVisible(false);
                    }
                    return false;
                }
                /*
                 *
                 * Invisible all cards when clicked button cards
                 *
                 * */
                for (var i = 0; i < me.ArrayButtonCard.length; i++) {
                    me.ArrayButtonCard[i].overlayer.setVisible(true);
                    me.ArrayButtonCard[i].setTouchEnabled(false);
                }

                //daifugo.serverHandler.dealCards(me.mSolutionCard[myiSolution]); // TODO: check solution before this step
                daifugo.serverHandler.contactServer('message', function () {
                    me.mButtonCard.setTouchEnabled(false);
                }, {action: "DEAL_CARD", dealCard: me.mSolutionCard[myiSolution]});
                console.log(me.mArrExchange);
                me.drawResult();
                this.mArrCheck = [];
            }
        }, cc.CONTROL_EVENT_TOUCH_UP_INSIDE);

        //------------------------------------//
        /*
         *
         *        PASS BUTTON
         *
         * */
        var me = this;
        var btnPassSprite = cc.Sprite.create(g_imageRes.s_btn_pass);
        this.mImgPassed = cc.Sprite.create(g_imageRes.image_passed);
        this.mImgPassed.setVisible(true);
        this.mImgPassed.setPosition(size.width * (616 / 960), size.height * (203 / 720));
        var action = cc.Sequence.create(cc.DelayTime.create(0.5),
            cc.CallFunc.create(this.removeFromParentAndCleanup, this.mImgPassed, true));

        me.btnPass = cc.ControlButton.create(btnPassSprite);
        me.btnPass.setTouchPriority(0);
        me.btnPass.setAdjustBackgroundImage(true);
        me.btnPass.setPosition(mWidthButtonPass, mHeightButton);
        me.btnPass.addTargetWithActionForControlEvents(this, function () {
            playSound('tap');
            if (me.canbeAction == true) {
                for (var i = 0; i < me.ArrayButtonCard.length; i++) {
                    me.ArrayButtonCard[i].overlayer.setVisible(true);
                    me.ArrayButtonCard[i].setTouchEnabled(false);
                    //me.ArrayButtonCard[i].opacity.setVisible(false);
                }
                me.mArrExchange = [];
                me.mTurnCard = [];
                if (me.ArrayButtonCard.length != 0) {
                    this.addChild(me.mImgPassed);
                    me.mImgPassed.runAction(action);
                }
                else {
                    return;
                }
                daifugo.serverHandler.contactServer('message', function () {
                }, {action: "DEAL_CARD", dealCard: me.mArrExchange});
            }
            else {
                return;
            }
            me.canbeAction = false;

            me.btnPass.setTouchEnabled(false);
        }, cc.CONTROL_EVENT_TOUCH_UP_INSIDE);
        this.addChild(me.btnPass);
    },
    /*
     *
     * Draw Result for user
     *
     * */
    drawResult: function (callback) {
        var me = this;
        var mImgUser = this.yourRank[this.imgRank.length - 1];
        var size = cc.Director.getInstance().getWinSize();
        mImgUser.setPosition(size.width * 0.291, size.height * 0.639);
        if (this.ArrayButtonCard.length == 0) {
            this.addChild(mImgUser);
            if(this.imgRank.length  > 1){
                playSound(g_Result[this.imgRank.length - 1]);
            }
            this.imgRank.pop();
        }

    }, reDrawCard: function (callback) {
        var size = cc.Director.getInstance().getWinSize();
        var wTop = size.width * (442.5 / 960);
        var hTop = size.height * (269 / 720);
        for (var i = 0; i < this.ArrayButtonCard.length; i++) {
            var mSprCard = cc.Sprite.createWithSpriteFrameName
            (convertColorCard(this.ArrayButtonCard[i].c) + "_" + convertRankCard(this.ArrayButtonCard[i].r) + ".png");
            var wCard = getSizeCCNode(mSprCard).getWidth() + 2;
            var hCard = getSizeCCNode(mSprCard).getHeight() + 2;
            this.ArrayButtonCard[i].runAction(
                cc.MoveTo.create(0.3, cc.p(wTop + wCard * ((i % 7) - 1),
                    hTop - hCard * Math.floor(i / 7))));
        }
    },
    /*
     *
     * Visible Button Card
     *
     * */

    reVisibleOverLayer: function (callback) {
        for (var i = 0; i < this.ArrayButtonCard.length; i++) {
            this.ArrayButtonCard[i].overlayer.setVisible(true);
            this.ArrayButtonCard[i].setTouchEnabled(false);
        }
        var mArrayCard = generateArr(this.mTurnCard, this.mSolutionCard);
        for (var i = 0; i < this.ArrayButtonCard.length; i++) {
            for (var j = 0; j < mArrayCard.length; j++) {
                for (var h = 0; h < mArrayCard[j].length; h++) {
                    if (this.ArrayButtonCard[i].index == mArrayCard[j][h].index) {
                        this.ArrayButtonCard[i].overlayer.setVisible(false);
                        this.ArrayButtonCard[i].setTouchEnabled(true);
                    }
                }
            }
        }

        //console.log(mArrayCard);
    },


    //--------------------------------//
    /*
     *
     *
     * Action when click button card
     *
     * */
    actionButtonCard: function () {
        var me = this;
        for (var i = 0; i < this.myCards.length; i++) {
            me.ArrayButtonCard[i].opacity.setZOrder(3);
            me.ArrayButtonCard[i].addTargetWithActionForControlEvents(this, function (target, action, controlEvents) {
                target.opacity.setVisible(!target.opacity._visible);
                if (target.opacity._visible == true) {
                    me.mButtonCard.setTouchEnabled(true);
                    this.mTurnCard.push(target.index);
                }

                else if (target.opacity._visible == false) {
                    for (var i = 0; i < this.mTurnCard.length; i++) {
                        if (this.mTurnCard[i] == target.index) {
                            this.mTurnCard.splice(i, 1);
                        }
                    }
                }

                me.reVisibleOverLayer();
                //target.addChild(target.opacity);
            }, cc.CONTROL_EVENT_TOUCH_UP_INSIDE);
        }


    },
    turnUser: function (ipPlayer) {
        if (ipPlayer != 3) {
            for (var i = 0; this.ArrayButtonCard.length; i++) {
                this.ArrayButtonCard.setTouchEnabled(false);
                this.ArrayButtonCard.overlayer.setVisible(true);
            }
        }
    },
    /*
     *
     * Draw CPU
     *
     * */
    CPUDraw: function () {
        var me = this;
        var size = cc.Director.getInstance().getWinSize();
        var wTopCPU1 = size.width * (85 / 960);
        var hTopCPU1 = size.height - size.height * (50 / 720);
        var hTOP = size.height - size.height * (170 / 720);
        var wTopCPU2 = size.width * (430 / 960);
        var hTopCPU2 = size.height - size.height * (50 / 720);
        var wTOPIconCPU2 = wTopCPU2 + size.width * 0.007;
        var wTopCPU3 = size.width * 0.798;
        var hTopCPU3 = size.height - size.height * (50 / 720);
        var wTOPIconCPU1 = wTopCPU1 + size.width * 0.007;
        var wTOPIconCPU3 = wTopCPU3 + size.width * (7 / 960);
        var arrValue = [
            {w: wTopCPU3, h: hTopCPU3, z: wTOPIconCPU3},
            {w: wTopCPU2, h: hTopCPU2, z: wTOPIconCPU2},
            {w: wTopCPU1, h: hTopCPU1, z: wTOPIconCPU1}
        ]

        var nameCPU = g_nameCPU.Japanesse;
        var nameCPU2 = shuffle(nameCPU);
        var mLabelCPU = [];
        var ImageCPU = [];
        this.mArrCPUCard = [];
        for (var i = 0; i < 3; i++) {
            var mArrCPU = [];
            for (var j = 0; j < me.ortherCards[i]; j++) {
                mArrCPU[j] = cc.Sprite.create(g_imageRes.s_card);
                var wCardCPU = getSizeCCNode(mArrCPU[j]).width / 2;
                var hCardCPU = getSizeCCNode(mArrCPU[j]).width * 2.7 / 3;
                mArrCPU[j].setPosition(arrValue[i].w + wCardCPU * ((j % 7) - 1), arrValue[i].h - hCardCPU * Math.floor(j / 7));
                me.addChild(mArrCPU[j]);
            }
            this.mArrCPUCard[i] = mArrCPU;
            mLabelCPU[i] = cc.LabelTTF.create(nameCPU2[i], "Times New Roman", 20);
            mLabelCPU[i].setPosition(arrValue[i].z + size.width * 0.016, hTOP);
            ImageCPU[i] = cc.Sprite.create(g_imageRes.s_player_npc)
            ImageCPU[i].setPosition(arrValue[i].z, hTOP);
            me.addChild(ImageCPU[i]);
            me.addChild(mLabelCPU[i]);
        }

    },
    /*
     *
     *    Action for CPU
     *
     * */

    actionCPU: function (ipPlayer, action, data, isTourFinish, tome, eightFlush, lock, rev, meEightFlush, speedMax) {

        // --- set value ------//
        var me = this;
        var size = cc.Director.getInstance().getWinSize();
        var wTopCPU1 = size.width * (85 / 960);
        var hTopCPU1 = size.height - size.height * (50 / 720);
        var hTOP = size.height - size.height * (170 / 720);
        var wTOPIconCPU1 = wTopCPU1 + size.width * (7 / 960);
        var wTopCPU2 = size.width * (430 / 960);
        var hTopCPU2 = size.height - size.height * (50 / 720);
        var wTOPIconCPU2 = wTopCPU2 + size.width * 0.007;
        var wTopCPU3 = size.width * 0.798;
        var hTopCPU3 = size.height - size.height * (50 / 720);
        var wTOPIconCPU3 = wTopCPU3 + size.width * 0.007;
        me.arrValue = [
            {w: wTopCPU3, h: hTopCPU3, z: wTOPIconCPU3},
            {w: wTopCPU2, h: hTopCPU2, z: wTOPIconCPU2},
            {w: wTopCPU1, h: hTopCPU1, z: wTOPIconCPU1}
        ]
        me.mImgPassedCPU = cc.Sprite.create(g_imageRes.image_passed_CPU);
        var actionPassCPU = cc.Sequence.create(cc.DelayTime.create(0.5),
            cc.CallFunc.create(this.removeFromParentAndCleanup, me.mImgPassedCPU, true));
        //------------------------------------------------//
        /*
         *
         * Invisible card on CPU and insert icon lockinghand
         *
         * */

        switch (3 - tome) {
            case 0 :
                this.ortherCards[0] = this.ortherCards[0] - data.length;
                if (this.mArrCPUCard[0].length != 0) {
                    for (var i = this.ortherCards[0]; i < this.mArrCPUCard[0].length; i++) {
                        this.mArrCPUCard[0][i].setVisible(false);
                    }
                }
                break;
            case 1 :
                this.ortherCards[1] = this.ortherCards[1] - data.length;
                if (this.mArrCPUCard[1].length != 0) {
                    for (var i = this.ortherCards[1]; i < this.mArrCPUCard[1].length; i++) {
                        this.mArrCPUCard[1][i].setVisible(false);
                    }
                }
                break;
            case 2 :
                this.ortherCards[2] = this.ortherCards[2] - data.length;
                if (this.mArrCPUCard[2].length != 0) {
                    for (var i = this.ortherCards[2]; i < this.mArrCPUCard[2].length; i++) {
                        this.mArrCPUCard[2][i].setVisible(false);
                    }
                }
                break;
        }

        /*
         *
         * Remove all deal card when finished turns
         *
         * */
        //for (var i = 0; i < me.arrCardDealed.length; i++) {
        //    me.arrCardDealed[i].overlayer.setVisible(true);
        //}

        if (isTourFinish == true) {
            var fn_remove = function () {
                for (var i = 0; i < me.arrCardDealed.length; i++) {
                    me.arrCardDealed[i].removeFromParent(true);
                }
            }
            if (meEightFlush) {
                cc.Sequence.create(
                    cc.DelayTime.create(1),
                    cc.CallFunc.create(function () {
                        fn_remove();
                    }.bind())
                );
            }
            else {
                fn_remove();
            }
        }

        /*
         *
         *  Action for CPU
         *
         * */


        if ((3 - tome) < 3) {

            switch (action) {
                case 0:
                    for (var i = 0; i < data.length; i++) {
                        if (data[i].mRank == cardJoker.Rank) {
                            data[i].mColor = cardJoker.Color;
                        }
                        var mOverLayer = cc.Sprite.create(g_imageRes.s_overlayer);
                        var mSprCPUAction = cc.Sprite.createWithSpriteFrameName(convertColorCard(data[i].mColor) + "_" + convertRankCard(data[i].mRank) + ".png")
                        mSprCPUAction.setPosition(me.arrValue[3 - tome].w, me.arrValue[3 - tome].h);
                        mSprCPUAction.setZOrder(2);
                        mSprCPUAction.overlayer = mOverLayer;
                        mSprCPUAction.overlayer.setVisible(false);
                        mSprCPUAction.addChild(mOverLayer, 2);
                        mOverLayer.setPosition(getSizeCCNode(mSprCPUAction).getWidth() / 2, getSizeCCNode(mSprCPUAction).getHeight() / 2);
                        if(speedMax == false){
                            playSound('cardshuffle');
                        }
                        actionMoveCard(mSprCPUAction, i);
                        me.arrCardDealed.push(mSprCPUAction);
                        this.addChild(mSprCPUAction);
                    }
                    break;
                case 1:
                    this.addChild(me.mImgPassedCPU);
                    me.mImgPassedCPU.setPosition(me.arrValue[3 - tome].z + size.width * 0.016, me.arrValue[3 - tome].h - size.height * (15 / 720));
                    me.mImgPassedCPU.runAction(actionPassCPU);
                    break;
            }
        }

        /*
         *
         * Set rank for CPU
         *
         * */

        me.mImgResult = [];
        if (me.ortherCards[3 - tome] == 0) {
            me.mImgResult[3 - tome] = me.imgRank[me.imgRank.length - 1];
            me.imgRank.pop();
            me.mImgResult[3 - tome].setPosition(me.arrValue[3 - tome].z + size.width * 0.016, me.arrValue[3 - tome].h - size.height * (15 / 720));
            me.addChild(me.mImgResult[3 - tome]);
        }


    },
    /*
     *
     *       Get cards from server
     *
     * */

    getCards: function (rData) {
        var me = this;
        var rank1 = cc.Sprite.create(g_imageRes.rank_1);
        var rank2 = cc.Sprite.create(g_imageRes.rank_2);
        var rank3 = cc.Sprite.create(g_imageRes.rank_3);
        var rank4 = cc.Sprite.create(g_imageRes.rank_4);
        me.imgRank = [rank4, rank3, rank2, rank1];

        var yourRank1 = cc.Sprite.create(g_imageRes.your_rank_1);
        var yourRank2 = cc.Sprite.create(g_imageRes.your_rank_2);
        var yourRank3 = cc.Sprite.create(g_imageRes.your_rank_3);
        var yourRank4 = cc.Sprite.create(g_imageRes.your_rank_4);
        me.yourRank = [yourRank4, yourRank3, yourRank2, yourRank1];
        me.arrCardDealed = [];
        var size = cc.Director.getInstance().getWinSize();
        me.lockiconOn = cc.Sprite.create(g_imageRes.lock_hand_on);
        me.revolutionOn = cc.Sprite.create(g_imageRes.revolution_on);
        me.lockiconOn.setVisible(false);
        me.revolutionOn.setVisible(false);
        me.addChild(me.lockiconOn);
        me.lockiconOn.setPosition(867, 490);
        me.revolutionOn.setPosition(867, 420);
        me.addChild(me.revolutionOn);

        me.sprite = cc.Sprite.create(g_imageRes.s_bg_lady);
        me.sprite.setAnchorPoint(0.5, 0.5);
        var w = getSizeCCNode(this.sprite).getWidth();
        var h = getSizeCCNode(this.sprite).getHeight();
        me.sprite.setPosition(w / 2, h / 2);
        me.addChild(me.sprite);
        me.myCards = rData.myCards;
        me.ortherCards = rData.ortherCards;
        me.mArrInvalidCard = [6, 7, 8];
        /*
         *
         * Set icon for game rules
         *
         * */
        me.drawCards();
        me.actionButtonCard();
        me.CPUDraw();

    },
    /*
     *
     * Get solution from server
     *
     * */
    getSolution: function (rData) {
        //playSound('player_turn');
        var me = this;
        this.time = 10;
        this.date = new Date();
        this.btnPass.setTouchEnabled(true);
        var size = cc.Director.getInstance().getWinSize();
        this.timeNow = this.date.getTime();
        //@function create the clock label
        var fn_create_clock = function () {
            // Create new label clock
            me.labelClock = cc.LabelTTF.create("", 'Times New Roman', 60);
            me.labelClock.setPosition(size.width * 0.291, size.height * 0.639);
            cc.Director.getInstance().
                getScheduler().
                scheduleCallbackForTarget(me.labelClock, function () {
                    var x = me.timer();
                    var k = me.timeNow - x;
                    var z = me.time + Math.round(k / 1000);
                    if (z < 0) {
                        z = 0;
                    }
                    me.labelClock.setString(z);
                }, 1, 10, 0, false);
            me.addChild(me.labelClock, 0);
        }
        // Display your turn notification
        me.imgYourTurn = cc.Sprite.create(g_imageRes.your_turn);
        var actionsolution = cc.Sequence.create(cc.DelayTime.create(1.2),
            cc.CallFunc.create(me.removeFromParent, me.imgYourTurn, true));
        me.mSolutionCard = rData;
        me.canbeAction = true;
        me.imgYourTurn.setPosition(size.width * 0.291, size.height * 0.639);
        me.addChild(me.imgYourTurn, 1);
        me.imgYourTurn.runAction(actionsolution);
        me.reVisibleOverLayer();
        // Destroy the clock label
        console.log("Label clock does not exist");
        fn_create_clock();
    },
    /*
     *
     * Get Status from server
     *
     * */

    getStatus: function (rData) {
        var me = this;
        me.canbeAction = false;
        this.btnPass.setTouchEnabled(false);
        me.size = cc.Director.getInstance().getWinSize();
        var ipPlayer = rData.iplayer;
        var action = rData.dealtype;
        var isTourFinish = rData.status.isTourFinish;
        var gameFinish = rData.status.gameFinish; // Exceed attribute --- Can remove
        var lock = rData.status.isBeginLock;
        var rev = rData.status.isBeginRevolution;
        var isRevo = rData.status.isRevolution;
        var data = rData.data;
        var tome = rData.toMe;
        var eightFlush = rData.status.isEightFlush;
        var speedMax = rData.status.isSpeedMax

        // TODO remove effect when Player finish game
        me.playerLockinghand = cc.Sprite.create(g_imageRes.player_lock_hand);
        me.playerLockinghand.setPosition(0, 300);
        me.playerLockinghand.setVisible(false);
        me.playerRvolution = cc.Sprite.create(g_imageRes.player_revolution);
        me.playerRvolution.setPosition(0, me.size.height / 2);
        me.playerRvolution.setVisible(false);
        me.actionCPU(ipPlayer, action, data, isTourFinish, tome, eightFlush, lock, rev,
            rData.me && eightFlush, speedMax);
        me.action = cc.Sequence.create(
            cc.MoveBy.create(0.5, cc.p(me.size.width / 2, 0)),
            cc.DelayTime.create(1),
            cc.MoveBy.create(0.5, cc.p(me.size.width, 0)),
            cc.CallFunc.create(me.playerLockinghand.removeFromParent(true), me.playerLockinghand, true)
        );
        if (tome == 0) {
            if (me.dealedCards && me.dealedCards.length > 0) {
                playSound('cardshuffle');
                for (var j = 0; j < me.dealedCards.length; j++) {
                    actionMoveCard(me.dealedCards[j], j);
                    me.arrCardDealed.push(me.dealedCards[j]);
                }
                me.dealedCards = [];
            }
            me.reDrawCard();
            me.mButtonCard.setTouchEnabled(false);
        }
        if ((3 - tome) != 3) {
            for (var i = 0; i < this.ArrayButtonCard.length; i++) {
                this.ArrayButtonCard[i].opacity.setVisible(false);
                this.ArrayButtonCard[i].overlayer.setVisible(true);
                this.ArrayButtonCard[i].setTouchEnabled(false);
                me.mButtonCard.setTouchEnabled(false);
            }
        }
        if (isTourFinish == true) {
            me.lockiconOn.setVisible(false);
        }
        if (me.lockiconOn._visible == false && isTourFinish == false) {
            me.lockiconOn.setVisible(lock);
        }
        me.revolutionOn.setVisible(isRevo);

        me.eightFlush = cc.Sprite.create(g_imageRes.image_8flush);
        var actionEightFlushIcon = cc.Sequence.create(cc.DelayTime.create(1.2),
            cc.CallFunc.create(this.removeFromParentAndCleanup, me.eightFlush, true));
        me.eightFlush.setPosition(me.size.width * (480 / 960), me.size.height * (360 / 720));
        if (lock == true && data.length > 0 && eightFlush == false) {
            if (tome == 0) {
                playSound('locking_hand_player');
                me.addChild(me.playerLockinghand);
                me.playerLockinghand.setVisible(true);
                me.playerLockinghand.runAction(me.action);
            }
            else {
                if (speedMax == false) {
                    playSound('locking_hand_cpu');
                }
            }
        }
        if (rev == true && data.length > 0 && eightFlush == false) {
            if (speedMax == false) {
                playSound('revolution');
            }
            me.addChild(me.playerRvolution, 3);
            me.playerRvolution.setVisible(true);
            me.playerRvolution.runAction(me.action);
        }
        if (eightFlush == true) {
            if (speedMax == false) {
                playSound('8_flush');
                {
                    if(tome == 0){
                        playSound('got_turn');
                    }
                }
            }
            me.eightFlush.runAction(actionEightFlushIcon);
            me.addChild(me.eightFlush, 3);
        }
        // Remove the labelClock
        if (me.labelClock) {
            console.log("Label clock exist");
            cc.Director.getInstance().getScheduler().unscheduleCallbackForTarget(me.labelClock, function () {
            });
            me.labelClock.removeFromParent(true);
        }
    },
    /*
     *
     * View result when finished game
     *
     * */
    getResult: function (rData) {
        var me = this;
        var result = rData.result;
        var size = cc.Director.getInstance().getWinSize();
        if (me.arrCardDealed.length != 0) {
            for (var i = 0; i < me.arrCardDealed.length; i++) {
                me.arrCardDealed[i].removeFromParent(true);
            }
        }
        var mImgUser = this.yourRank[this.imgRank.length - 1];
        mImgUser.setPosition(size.width * 0.291, size.height * 0.639);
        if (me.ArrayButtonCard.length != 0) {
            me.addChild(mImgUser);
            if(this.imgRank.length  > 1){
                playSound(g_Result[this.imgRank.length - 1]);
            }
        }
        me.btnPass.setTouchEnabled(false);
        me.mButtonCard.setTouchEnabled(false);
        var mSprbtnQuit = cc.Sprite.create(g_imageRes.btn_quit);
        me.btnQuit = cc.ControlButton.create(mSprbtnQuit);
        me.btnQuit.setTouchPriority(0);
        me.btnQuit.setAdjustBackgroundImage(true);
        me.btnQuit.setPosition(size.width / 2, size.height / 2);
        me.btnQuit.addTargetWithActionForControlEvents(this, function () {
            playSound('tap');
            var nextScene = cc.Scene.create();
            var nextLayer = new TopScene;
            nextScene.addChild(nextLayer);
            cc.Director.getInstance().replaceScene(nextScene);
        }, cc.CONTROL_EVENT_TOUCH_DOWN)
        me.addChild(me.btnQuit, 10);
    },
    timer: function () {
        var date = new Date();
        var timeUpdate = date.getTime();
        return timeUpdate;
    }
});


